﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar; 
using MyHotel.MyHotel_MHMain_BUS_Service;
using MyHotel.MyHotel_MHDangNhap_BUS_Webservice;
using MyHotel.MyHotel_MHPhong_BUS_Webservice;
using System.Threading;

namespace MyHotel
{
    public partial class frmMain : Office2007RibbonForm
    {
        private MyHotel.MyHotel_MHMain_BUS_Service.Menu[] listMenu;
        private NhanVien _nhanvien;
        private int _maChucVu;
        private string _user;
        private List<MyForm> lstwindows; 
        private int _nphongtrong;
        private int _nphongdangthue;  

        private int Phongtrong
        {
            get
            {
                return _nphongtrong;
            }
            set
            { 
                _nphongtrong = value;

                progressBarPhongDangThue.Maximum = Phongtrong + Phongdangthue;
                progressBarPhongTrong.Maximum = Phongtrong + Phongdangthue;
                progressBarPhongTrong.Value = Phongtrong;
                progressBarPhongDangThue.Value = Phongdangthue;

                lblPhongTrong.Text = "Phòng Trống : " + Phongtrong.ToString() + "/" + (Phongtrong + Phongdangthue).ToString();
                lblPhongDangThue.Text = "Đang Thuê : " + Phongdangthue.ToString() + "/" + (Phongtrong + Phongdangthue).ToString();
            } 
        }

        private int Phongdangthue
        {
            get
            {
                return _nphongdangthue;
            }
            set
            { 
                _nphongdangthue = value;

                progressBarPhongDangThue.Maximum = Phongtrong + Phongdangthue;
                progressBarPhongTrong.Maximum = Phongtrong + Phongdangthue;
                progressBarPhongTrong.Value = Phongtrong;
                progressBarPhongDangThue.Value = Phongdangthue;

                lblPhongTrong.Text = "Phòng Trống : " + Phongtrong.ToString() + "/" + (Phongtrong + Phongdangthue).ToString();
                lblPhongDangThue.Text = "Đang Thuê : " + Phongdangthue.ToString() + "/" + (Phongtrong + Phongdangthue).ToString();
            }
        }



        public frmMain(int machucvu, string user)
        {
            _maChucVu = machucvu;
            _user = user;
            lstwindows = new List<MyForm>();
            lstwindows.Add(new frmPhong());
            lstwindows.Add(new frmDanhMucPhong());
            lstwindows.Add(new frmQuanLyNhanVien());
            InitializeComponent();
        }

        public frmMain(MyHotel.MyHotel_MHDangNhap_BUS_Webservice.NhanVien nv)
        {
           _nhanvien = nv; 
            lstwindows = new List<MyForm>();
            lstwindows.Add(new frmPhong());
            lstwindows.Add(new frmDanhMucPhong());
            InitializeComponent();
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            this.styleManager1.ManagerStyle = DevComponents.DotNetBar.eStyle.Office2010Black;
        }

        private void office2007StartButton1_Click(object sender, EventArgs e)
        {
             
        }

        private void OpenWindows(int index)
        {
            int n = lstwindows.Count;
            if (index >= 0 && index < n)
            {
                MyForm frm = (MyForm)findChild(lstwindows[index].getName());
                if (frm != null)
                {
                    frm.WindowState = FormWindowState.Normal; 
                    frm.Focus();
                    return;
                }
                this.Cursor = Cursors.WaitCursor;
                frm = lstwindows[index].Clone();
                frm.MdiParent = this;
                
                frm.Show();
                this.Cursor = Cursors.Default;
            }
        }
 
        private void LoadMenu()
        {
            MHMainBUSWSSoapClient ws = new MHMainBUSWSSoapClient();
            listMenu = ws.getListMenu();
            foreach (Control ctrl in ribbonControl1.Controls)
            {
                if (ctrl != null)
                {
                    MessageBox.Show(ctrl.Tag.ToString());
                }
            }
        }

        private void btn_tracuuphong_Click(object sender, EventArgs e)
        {
            OpenWindows(0);
        }

        private void frmMain_Load(object sender, EventArgs e)
        { 
            MHMainBUSWSSoapClient ws = new MHMainBUSWSSoapClient();
            listMenu = ws.getMenuByMaNV(_nhanvien.Manv); 
            foreach (Control item in ribbonPanel1.Controls)
            {
                RibbonBar tab = item as RibbonBar;
                if (tab != null)
                {
                    foreach (MyHotel.MyHotel_MHMain_BUS_Service.Menu mnu in listMenu)
                    {
                        if (tab.Tag != null && tab.Tag.ToString().Trim().Equals(mnu.GiatriMenu))
                        {
                            tab.Visible = true;
                            break;
                        }
                    }
                }
            }

            LoadListPhong();

            flowLayoutPanelNote.Controls.Add(new RoomStateBlank().getNoteRoom());
            flowLayoutPanelNote.Controls.Add(new RoomStateHiring().getNoteRoom()); 
        }

        private delegate void AddPhong(uscRoom room);

        private void themPhong(uscRoom room)
        {
            if (this.flowLayoutPanelPhong.InvokeRequired)
            {
                AddPhong d = new AddPhong(themPhong);
                this.Invoke(d, new object[] { room });
            }
            else
            {
                flowLayoutPanelPhong.Controls.Add(room);
            } 
        }

        private void LoadListPhong()
        {
            flowLayoutPanelPhong.Controls.Clear();
            Thread thread = new Thread(new ThreadStart(LayDanhSachPhong));
            thread.Start();
        }

        private void LayDanhSachPhong()
        {
            MHPhongBUSWSSoapClient ws1 = new MHPhongBUSWSSoapClient();
            Phong[] array = ws1.getListPhong();

            foreach (Phong p in array)
            {
                String s = p.TinhTrang;
                StateRoomManager manager = new StateRoomManager();
                StateRoom state = manager.CreateStateRoom(p.TinhTrang);
                uscRoom room = new uscRoom(p, state);
                room.ChangeState += new uscRoom.StateChange(room_ChangeState);

                this.themPhong(room);
            } 
        }


        private void ribbonTabItem5_Click(object sender, EventArgs e)
        {
          
        }
 
        private void btn_lapdanhmucphong_Click(object sender, EventArgs e)
        {
            OpenWindows(1);
        }

        private void btn_baocaodoanhthu_Click(object sender, EventArgs e)
        {  
        }

        private void buttonItem4_Click(object sender, EventArgs e)
        { 
           
        }

        private void btn_baocaomatdo_Click(object sender, EventArgs e)
        { 
        }

        private void btn_phieuthuephong_Click(object sender, EventArgs e)
        {
            
        }

        private void buttonItem6_Click(object sender, EventArgs e)
        {

        }

        private void bar2_ItemClick(object sender, EventArgs e)
        {

        }

        Office2007RibbonForm findChild(string name)
        {
            foreach (Office2007RibbonForm frmChild in this.MdiChildren)
            {
                if (frmChild.Name.Equals(name))
                {
                    return frmChild;
                }
            }
            return null;
        }

        private void btn_Quydinh_Click(object sender, EventArgs e)
        {
           
        }

        private void btnQuanLyNhanVien_Click(object sender, EventArgs e)
        {
            
        }
        private void btnItemThoat_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có muốn thoát chương trình quản lý khách sạn không?", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
            if (dr == DialogResult.Yes)
            {
                this.Close();
            } 
        }

        private void btnItemDangXuat_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnTroGiup_Click(object sender, EventArgs e)
        {
            try
            {
                string path = Application.StartupPath + "\\help.htm";
                System.Diagnostics.Process.Start(path);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            } 
        } 

        private void buttonItemDanhMucPhong_Click(object sender, EventArgs e)
        {
            frmDanhMucPhong frm = new frmDanhMucPhong();
            frm.ThemPhong_event += new frmDanhMucPhong.ThemPhong(frm_ThemPhong_event);
            frm.SuaPhong_event += new frmDanhMucPhong.SuaPhong(frm_SuaPhong_event);
            frm.XoaPhong_event += new frmDanhMucPhong.XoaPhong(frm_XoaPhong_event);
            frm.ShowDialog(); 
        }

        void frm_XoaPhong_event(MyHotel_MHLapDanhMucPhong_BUS_Webservice.Phong p)
        {
            int count = flowLayoutPanelPhong.Controls.Count;
            for (int i = 0; i < count; i++)
			{
			  uscRoom room = flowLayoutPanelPhong.Controls[i] as uscRoom;
                if (room != null)
                {
                    if (room.Phong.TenPhong == p.TenPhong)
                    {
                        flowLayoutPanelPhong.Controls.RemoveAt(i);
                        break;
                    }
                }
			} 
        }

        void frm_SuaPhong_event(MyHotel_MHLapDanhMucPhong_BUS_Webservice.Phong p)
        {
            foreach (Control ctrl in flowLayoutPanelPhong.Controls)
            {
                uscRoom room = ctrl as uscRoom;
                if (room != null)
                {
                    if (room.Phong.MaPhong == p.MaPhong)
                    {
                        Phong phong = new Phong();
                        phong.MaPhong = p.MaPhong;
                        phong.TenPhong = p.TenPhong;
                        phong.LoaiPhong = p.LoaiPhong;
                        phong.GhiChu = p.GhiChu;
                        phong.TinhTrang = p.TinhTrang;
                        phong.TienPhong = p.TienPhong;

                        room.Phong = phong;
                        break;
                    } 
                }
            }
        }

        void frm_ThemPhong_event(MyHotel_MHLapDanhMucPhong_BUS_Webservice.Phong p)
        {
            Phong phong = new Phong();
            phong.MaPhong = p.MaPhong;
            phong.TenPhong = p.TenPhong;
            phong.LoaiPhong = p.LoaiPhong;
            phong.GhiChu = p.GhiChu;
            phong.TinhTrang = p.TinhTrang;
            phong.TienPhong = p.TienPhong; 
             
            uscRoom room = new uscRoom(phong, new RoomStateBlank());
            room.ChangeState += new uscRoom.StateChange(room_ChangeState);
            flowLayoutPanelPhong.Controls.Add(room);
        }

        void room_ChangeState(StateRoom state)
        {
            RoomStateHiring hiring = state as RoomStateHiring;
            if (hiring != null)
            {
                Phongdangthue++;
                Phongtrong--;
            }
            else
            {
                Phongtrong++;
                Phongdangthue--; 
            }
        }

        private void buttonItemQuanLyNhanVien_Click(object sender, EventArgs e)
        {
            frmBaoCaoDoanhThu frm = new frmBaoCaoDoanhThu();
            frm.Show();
        }

        private void flowLayoutPanelPhong_ControlAdded(object sender, ControlEventArgs e)
        {  
            uscRoom room = e.Control as uscRoom;
            if (room != null)
            {
                RoomStateHiring hiring = room.State as RoomStateHiring;
                if (hiring != null)
                {
                    Phongdangthue++;
                }
                else
                {
                    Phongtrong++;
                }

            }
        }

        private void flowLayoutPanelPhong_ControlRemoved(object sender, ControlEventArgs e)
        {
            uscRoom room = e.Control as uscRoom;
            if (room != null)
            {
                RoomStateHiring hiring = room.State as RoomStateHiring;
                if (hiring != null)
                {
                    Phongdangthue--;
                }
                else
                {
                    Phongtrong--;
                }

            }
        }

        private void buttonItem8_Click(object sender, EventArgs e)
        {
            frmQuanLyNhanVien frm = new frmQuanLyNhanVien();
            frm.ShowDialog();
        }

        private void buttonItem9_Click(object sender, EventArgs e)
        {
            frmAuthorization frm = new frmAuthorization();
            frm.ShowDialog();
        }

        private void buttonItem6_Click_2(object sender, EventArgs e)
        {
            styleManager1.ManagerStyle = eStyle.Office2010Blue;
        }

        private void buttonItem7_Click(object sender, EventArgs e)
        {
            styleManager1.ManagerStyle = eStyle.Office2010Silver;
        }

        private void buttonItem11_Click(object sender, EventArgs e)
        {
            frmBaoCaoDoanhThu frm = new frmBaoCaoDoanhThu();
            frm.ShowDialog();
        }

        private void buttonItem12_Click(object sender, EventArgs e)
        {
            frmBaoCaoMatDo frm = new frmBaoCaoMatDo();
            frm.ShowDialog();
        }

        private void buttonItem1_Click_1(object sender, EventArgs e)
        {
            MessageBox.Show("Xin lỗi. Vì không đủ thời gian, nên chức năng này chưa được hoàn thành.");
        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Vì không đủ thời gian, chức năng này chưa được hoàn thành.");
        }
 
    }
}